package com.acme.bankapp.domain.bank;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.acme.bankapp.domain.account.Account;
import com.acme.bankapp.listeners.email.Email;
import com.acme.bankapp.service.bank.Gender;

public class Client {
	private Gender gen;
	private String name;
	private int age;
	private Account primaryacc;
	private Email email;
	private List<Account> accounts = new ArrayList<Account>();

	@Override
	public String toString() {
		StringBuilder info = new StringBuilder("Client: ");
		info.append("[name="+gen+name);
		info.append(", age="+age);
		info.append(" ,acc=");
		if (primaryacc != null){
			info.append(primaryacc.getAccountType());
		}
		else{
			info.append("Client does not have any account");
		}
		info.append("]");
		return info.toString();
	}

	@Override
	public int hashCode() {
		return primaryacc.hashCode();
	}

	@Override
	public boolean equals(Object obj) {
		System.out.println("Abstractaccount");
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Client other = (Client) obj;
		if (primaryacc == null) {
			if (other.getAcc(0) != null)
				return false;
		} else if (!primaryacc.equals(other.getAcc(0)))
			return false;
		if (age != other.getAge())
			return false;
		if (name == null) {
			if (other.getName() != null)
				return false;
		} else if (!name.equals(other.getName()))
			return false;
		return true;
	}

	public String getClientSalutation() {
		return gen.toString() + name;
	}

	public Client(String name, Gender gen, int age) {
		this.name = name;
		this.age = age;
		this.gen = gen;
	}

	public Account getAcc() {
		return primaryacc;
	}

	public Account getAcc(int id) {
		for (Account ac : accounts) {
			if (ac.hashCode() == id) {
				return ac;
			}
		}
		return null;
	}

	public List<Account> getAccList() {
		return Collections.unmodifiableList(accounts);
	}

	public void addAcc(Account acc) {
		if (accounts.isEmpty()) {
			primaryacc = acc;
		}
		accounts.add(acc);
	}

	public int getAge() {
		return age;
	}

	public String getName() {
		return name;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Email getEmail() {
		return email;
	}

	public void setEmail(Email email) {
		this.email = email;
	}
	
}
